package com.gitee.wsl.compose.ui.section.lazylist.item

import androidx.compose.foundation.LocalIndication
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.unit.Dp
import com.gitee.wsl.compose.ui.section.CupertinoSectionDefaults
import com.gitee.wsl.compose.ui.section.CupertinoSectionTokens
import com.gitee.wsl.compose.ui.section.lazylist.ContentTypeLabel
import com.gitee.wsl.compose.ui.section.lazylist.LazySectionScope
import com.gitee.wsl.compose.ui.section.lazylist.row


fun LazySectionScope.LabelWithCustomChevron(
    chevron: @Composable RowScope.() -> Unit,
    onClick: () -> Unit,
    key: Any? = null,
    enabled: Boolean = true,
    icon: @Composable() (() -> Unit)? = null,
    dividerPadding: Dp = if (icon != null)
        CupertinoSectionDefaults.DividerPaddingWithIcon
    else CupertinoSectionDefaults.DividerPadding,
    onClickLabel: String? = null,
    interactionSource: MutableInteractionSource? = null,
    title: @Composable () -> Unit,
) = row(
    key = key,
    contentType = ContentTypeLabel,
    dividerPadding = dividerPadding,
    modifier = {
        Modifier
            .clickable(
                enabled = enabled,
                onClick = onClick,
                role = Role.Button,
                onClickLabel = onClickLabel,
                interactionSource = interactionSource ?: remember { MutableInteractionSource() },
                indication = LocalIndication.current
            )
    },
    title = {

        val color = if (enabled)
            MaterialTheme.colorScheme.primary
        else MaterialTheme.colorScheme.secondary

        CompositionLocalProvider(
            LocalContentColor provides color
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(CupertinoSectionTokens.HorizontalPadding)
            ) {
                icon?.invoke()
                title()
            }
        }
    },
    endContent = {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(CupertinoSectionTokens.InlinePadding),
            content = chevron
        )
    }
)